package com.hsgene.order.constants;

import com.hsgene.common.util.exception.ErrorCode;
import com.hsgene.order.exception.OrderErrorCode;

/**
 * 商城订单状态枚举
 *
 * @author wangbing
 * @version 1.0, 2018/8/3
 */
public enum OrderMallStatus {

    /**
     * 待付款
     */
    INIT("init", "待付款"),

    /**
     * 待发货
     */
    PAID("paid", "待发货"),

    /**
     * 待收货
     */
    DELIVERED("delivered", "待收货"),

    /**
     * 待评论
     */
    CONFIRMED_CONSIGN("confirmed_consign", "待评论"),

    /**
     * 已完成
     */
    DONE("done", "已完成"),

    /**
     * 已取消
     */
    CANCELED("canceled", "已取消"),

    /**
     * 已失效
     */
    DISABLED("disabled", "已失效"),

    /**
     * 未定义的商城订单状态
     */
    UNDEFINED("undefined", "未定义的商城订单状态");

    /**
     * 编码
     */
    private final String code;

    /**
     * 描述
     */
    private final String desc;

    public static OrderMallStatus getEnum(String code) {
        for (OrderMallStatus status : values()) {
            if (status.code.equalsIgnoreCase(code)) {
                return status;
            }
        }

        return UNDEFINED;
    }

    OrderMallStatus(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public String getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }

    /**
     * 根据状态自检订单是否可以支付
     *
     * @return 错误提示信息
     */
    public ErrorCode checkPayment() {
        if (OrderMallStatus.INIT.equals(this)) {
            return null;
        } else if (UNDEFINED.equals(this)) {
            return OrderErrorCode.ORDER_STATUS_EXCEPTION;
        } else if (DISABLED.equals(this)) {
            return OrderErrorCode.ORDER_STATUS_PAY_FOR_DISABLED;
        } else if (CANCELED.equals(this)) {
            return OrderErrorCode.ORDER_STATUS_PAY_FOR_CANCELED;
        } else {
            return OrderErrorCode.ORDER_STATUS_PAY_FOR_PAID;
        }
    }
}
